import 'package:flutter/material.dart';
import 'package:flutter_food/core/router/router.dart';
import 'package:flutter_food/core/viewmodel/filter_view_model.dart';
import 'package:flutter_food/core/viewmodel/meal_view_model.dart';
import 'package:flutter_food/core/viewmodel/favor_view_model.dart';
import 'package:flutter_food/ui/shared/app_theme.dart';
import 'package:flutter_food/ui/shared/size_fit.dart';
import 'package:provider/provider.dart';

void main() {
  // app运行时，加载全局数据共享
  // 此处为懒加载，在第一次使用时，才发送网络请求
  runApp(MultiProvider(
    child: MyApp(),
    providers: [
      ChangeNotifierProvider(create: (ctx) => FilterViewModel()),
      // MealViewModel需要使用FilterViewModel做过滤，使用代理建立两者依赖
      ChangeNotifierProxyProvider<FilterViewModel, MealViewModel>(
        create: (ctx) => MealViewModel(),
        update: (ctx, filterVM, mealVM) {
          mealVM.updateFilterViewModel(filterVM);
          return mealVM;
        },
      ),
      ChangeNotifierProxyProvider<FilterViewModel, FavorViewModel>(
          create: (ctx) => FavorViewModel(),
          update: (ctx, filterVM, favorVM) {
            favorVM.updateFilterViewModel(filterVM);
            return favorVM;
          }),
    ],
  ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    SizeFit.init();

    return MaterialApp(
      title: '美食广场',
      theme: AppTheme.normalTheme,
      initialRoute: AppRouter.initRoute,
      routes: AppRouter.routes,
      onGenerateRoute: AppRouter.onGenerateRoute,
    );
  }
}
